package adapter;

import java.util.ArrayList;

import utils.Event;
import viewcontroller.SlideMenu;

import com.example.lastprojectbeautiplus.R;
import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeViewPagerAdapter extends PagerAdapter {
	private ArrayList<Event> eventList;
	private Activity activity;
	private ImageView imgEvent;
	private TextView tvEventDescription;
	private View view;

	public HomeViewPagerAdapter(Activity activity, ArrayList<Event> eventList) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
		this.eventList = eventList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return eventList.size();
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.home_view_pager_item, null);
		imgEvent = (ImageView) view.findViewById(R.id.imgEvent);
		tvEventDescription = (TextView) view
				.findViewById(R.id.tvEventDescription);
		tvEventDescription.setText("");
		String imgPath = eventList.get(position).getEventImage();
		Picasso.with(activity).load(imgPath).placeholder(R.drawable.image_wait)
				.error(R.drawable.error_loading).into(imgEvent);

		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(v.getContext(), SlideMenu.class);
				intent.putExtra("EventID", eventList.get(position).getEventID());
				v.getContext().startActivity(intent);
			}
		});

		((ViewPager) container).addView(view, 0);
		return view;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView((View) arg2);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == ((View) arg1);
	}

	@Override
	public Parcelable saveState() {
		return null;
	}
}
